package com.yidu.t299.controller.ssk;
import com.yidu.t299.domain.ssk.PerformanceStat;
import com.yidu.t299.service.ssk.PerformanceStatBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 粟术康
 * @version 1.0
 * @description
 * @date:2023/3/10 10:53
 */
@RestController
public class PerformanceStatController {
    @Autowired
    private PerformanceStatBiz performanceStatBiz;

    /**
     * 分页查询显示数据在表格中
     *
     * @param limit 页码
     * @param page  每页的行数
     * @param performanceStat   接收多条件搜索的表单数据对象
     * @return 映射集合对象
     */
    @GetMapping("perfpemancestat/restful_perfpemancestat")
    public Map<String, Object> findAll(int limit, int page, PerformanceStat performanceStat) {
        // 根据school对象得到条件字符串
        String condition = performanceStatBiz.getCondition(performanceStat);
        // 按条件统计总记录行数
        int count = performanceStatBiz.selectCount(condition);
        // 按条件分页查询
        List<PerformanceStat> performanceStatList = performanceStatBiz.selectByPage(limit, page-1, condition);
        // 创建一个map，用来存放返回给layui组件需要的键值对
        Map<String, Object> mapData = new HashMap<String, Object>();
        //在映射集合中添加key为code，值为0的状态码
        mapData.put("code", 0);
        //在映射集合中添加key为msg，值为空字符串的响应信息字符串
        mapData.put("msg", "");
        //在映射集合中添加为count，值为总行数的映射
        mapData.put("count", count);
        //在映射集合中添加key为data，值为校区对象集合的映射
        mapData.put("data", performanceStatList);
        //把映射集合响应到客户端
        return mapData;
    }

    /**
     * 添加一个新绩效
     *
     * @param performanceStat 绩效对象：其前要跟注解 @RequestBody，因为前端页面提交过来的数据就是JSON格式的字符串
     * @return 提示信息字符串
     */
    @PostMapping("perfpemancestat/restful_perfpemancestat")
    public String addEmp(@RequestBody PerformanceStat performanceStat) {
        System.out.println(performanceStat);
        if(performanceStat!=null){
            if(performanceStat.getStudentName()!=null && performanceStat.getRecordName()!=null && performanceStat.getClassName()!=null){
                performanceStat.setStudentId(performanceStatBiz.selectByStudentName(performanceStat.getStudentName()));
                performanceStat.setRecordId(performanceStatBiz.selectByRecordName(performanceStat.getRecordName()));
                performanceStat.setClassId(performanceStatBiz.selectByClassName(performanceStat.getClassName()));
                //调用业务层实现新增成绩，并返回正确与否字符串
                return performanceStatBiz.insert(performanceStat) > 0 ? "add-success" : "add-failure";
            }
        }
        //调用业务层实现新增活动，并返回正确与否字符串
        return "add-failure";
    }

    /**
     * 修改绩效信息
     *
     * @param performanceStat 绩效对象
     * @return 提示信息字符串
     */
    @PutMapping("perfpemancestat/restful_perfpemancestat")
    public String updateEmp(@RequestBody PerformanceStat performanceStat) {
        System.out.println(performanceStat);
        if(performanceStat!=null){
            if(performanceStat.getStudentName()!=null && performanceStat.getRecordName()!=null && performanceStat.getClassName()!=null){
                //查询出学员编号和课程编号，存入charge对象中
                performanceStat.setStudentId(performanceStatBiz.selectByStudentName(performanceStat.getStudentName()));
                performanceStat.setRecordId(performanceStatBiz.selectByRecordName(performanceStat.getRecordName()));
                performanceStat.setClassId(performanceStatBiz.selectByClassName(performanceStat.getClassName()));
                //调用业务层实现新增成绩，并返回正确与否字符串
                return performanceStatBiz.updateByPrimaryKey(performanceStat) > 0 ? "update-success" : "update-failure";
            }
        }
        //调用业务层实现修改校区，并返回正确与否字符串
        return "update-failure";
    }

    /**
     * 根据绩效编号删除活动
     *
     * @param perId 绩效编号
     * @return 提示信息字符串
     */
    @DeleteMapping("perfpemancestat/restful_perfpemancestat/{perId}")
    public String deleteEmpById(@PathVariable("perId") int perId) {
        //调用业务层实现删除活动，并返回正确与否字符串
        return performanceStatBiz.deleteByPrimaryKey(perId) > 0 ? "delete-success" : "delete-failure";
    }

    /**
     * 根据多个绩效编号批量删除绩效
     * 此方法使用put请求替代delete，put可以带请求体
     *
     * @param perIdStr 多个绩效编号字符串
     * @return 提示信息字符串
     */
    @PutMapping("perfpemancestat/batchDel")
    public String deleteEmpByManyId(@RequestParam String perIdStr) {
        //按,号分割成字符串数组
        String[] strArrays = perIdStr.split(",");
        //循环按市场编号删除
        for (String str : strArrays) {
            //只要有一个删除错误就直接返回错误信息字符串
            if (performanceStatBiz.deleteByPrimaryKey(Integer.parseInt(str)) < 1) {
                return "delete-failure";
            }
        }
        return "delete-success";
    }
}
